//
//  M1RecentFriendsVC.m
//  NewMarryU
//
//  Created by Cedar on 2018/7/19.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1RecentFriendsVC.h"
#import "M1RecentFriendCell.h"

#import "M1HomeDetailsVC.h"
#import "M1HomeModel.h"
#import "M1MoreVipCell.h"
#import "MJRefresh.h"
#import "UITableView+Placeholder.h"

@interface M1RecentFriendsVC ()

@property (nonatomic, strong) NSMutableArray *m1_data;

@property (nonatomic, assign) NSInteger m1_page;

@end

@implementation M1RecentFriendsVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"访问我的";
    
    _m1_page = 1;
    
    _m1_data = [[NSMutableArray alloc] init];
        
    [self.m1_tableView registerClass:[M1RecentFriendCell class] forCellReuseIdentifier:m1_tableViewCellIdentifier];
    [self.m1_tableView registerClass:[M1MoreVipCell class] forCellReuseIdentifier:@"morecell"];
    self.m1_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.m1_page = 1;
        [self.m1_tableView.mj_header beginRefreshing];
        [self m1_requestList];
    }];
    
    self.m1_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.m1_page++;
        [self.m1_tableView.mj_footer beginRefreshing];
        [self m1_requestList];
    }];
    
    [self.m1_tableView.mj_header beginRefreshing];
    
}

#pragma mark - request
- (void)m1_requestList
{
    NSDictionary *dic = @{@"page":@(_m1_page)};
    WEAKSELF;
    [M1HttpNet requestWithMethod:m1_visit_me_url params:dic success:^(id resDic, NSInteger code, NSString *message) {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        for (NSDictionary *dic in resDic[@"user"])
        {
            M1User *user = [M1User mj_objectWithKeyValues:dic];
            [array addObject:user];
        }
        if (weakSelf.m1_page == 1) {

            weakSelf.m1_data = [NSMutableArray arrayWithArray:array];
            if (weakSelf.m1_data.count >5&&[M1User user].status.member_fees_status!=1) {
                [weakSelf.m1_data insertObject:@"more" atIndex:5];
            }
        } else {
            [weakSelf.m1_data addObjectsFromArray:[array copy]];
        }
        
        weakSelf.m1_tableView.imgName = @"data_none";
        weakSelf.m1_tableView.noneTitle = @"空空如也";
        weakSelf.m1_tableView.m1_needPlace = @(1);
        
        [weakSelf.m1_tableView reloadData];
        [weakSelf.m1_tableView.mj_header endRefreshing];
        [weakSelf.m1_tableView.mj_footer endRefreshing];
    } fail:^(id resDic, NSInteger code, NSString *message) {
        [M1Alert m1_alertWithTitle:nil message:message];
        [weakSelf.m1_tableView.mj_header endRefreshing];
        [weakSelf.m1_tableView.mj_footer endRefreshing];
    } showAni:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.m1_data.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.m1_data[indexPath.row] isKindOfClass:[NSString class]]) {
        return 100;
    }
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.m1_data[indexPath.row] isKindOfClass:[NSString class]]) {
        M1MoreVipCell *m1_vipCell = [tableView dequeueReusableCellWithIdentifier:@"morecell"];
        return m1_vipCell;
    }
    M1RecentFriendCell *cell = [tableView dequeueReusableCellWithIdentifier:m1_tableViewCellIdentifier];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.m1_user = self.m1_data[indexPath.item];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 5 &&[M1User user].status.member_fees_status !=1) {
        [self m1_goVipVC];
        return;
    }
    
    
    if (indexPath.row > 5&&[M1User user].status.member_fees_status !=1) {
        WEAKSELF;
        [M1Alert m1_alertWithTitle:nil message:@"只有加V会员才能查看更多信息" buttonTitles:@[@"取消",@"立即加入"] action:^(NSInteger index) {
            if (index == 1) {
                [weakSelf m1_goVipVC];
            }
        }];
        
        return;
    }
    
    M1User *userM = self.m1_data[indexPath.row];
    M1HomeModel *m1_homeModel = [M1HomeModel new];
    M1HomeBasic *m1_basic = [M1HomeBasic new];
    m1_basic.uid = userM.basic.uid;
    m1_homeModel.basic = m1_basic;
    M1HomeDetailsVC *m1_detailVC = [[M1HomeDetailsVC alloc]init];
    m1_detailVC.m1_hiddenNav = YES;
    m1_detailVC.m1_model = m1_homeModel;
    [self.navigationController pushViewController:m1_detailVC animated:YES];
}

-(void)m1_goVipVC{
    M1VIPCenterVC *m1_vipVC = [[M1VIPCenterVC alloc]init];
    [self.navigationController pushViewController:m1_vipVC animated:YES];
    WEAKSELF;
    m1_vipVC.buyVip = ^(BOOL isSuccess) {
        [weakSelf.m1_data removeObjectAtIndex:5];
        [weakSelf.m1_tableView reloadData];
    };
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
